import request from "@/utils/request";

/**
 * 分页查询角色
 */
export async function pageRoles(params) {
  const res = await request.get("/system/role/page", { params });
  if (res.data.code === 200) {
    return res.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 查询角色列表
 */
export async function listRoles(params) {
  const res = await request.get("/system/role", {
    params,
  });
  if (res.data.code === 200 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 根据id查询用户
 */
export async function getRole(id) {
  const res = await request.get("/system/role/" + id);
  if (res.data.code === 200 && res.data.data) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 添加角色
 */
export async function addRole(data) {
  const res = await request.post("/system/role", data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改角色
 */
export async function updateRole(data) {
  const res = await request.put("/system/role/" + data.role_id, data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 删除角色
 */
export async function removeRole(id) {
  const res = await request.delete("/system/role/" + id);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 批量删除角色
 */
export async function removeRoles(data) {
  const res = await request.delete("/system/role/batch", {
    data,
  });
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 获取角色分配的菜单
 */
export async function listRoleMenus(role_id) {
  const res = await request.get("/system/role-menu/" + role_id);
  if (res.data.code === 200) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.msg));
}

/**
 * 修改角色菜单
 */
export async function updateRoleMenus(role_id, data) {
  const res = await request.put("/system/role-menu/" + role_id, data);
  if (res.data.code === 200) {
    return res.data.msg;
  }
  return Promise.reject(new Error(res.data.msg));
}
